package com.anzhixin.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.anzhixin.action.DownLoadActionListener;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1576636764607009584L;
	private JLabel lblStatus;

	private JTextArea txtLog = new JTextArea(5, 20);
	private JScrollPane logScrollPane;
	
	private JProgressBar progressBarAll;
	private JProgressBar progressBarCurr;
	private JButton btnDown;

	private JButton btnExit;
	private JButton btnDownloadDetail;

	/**
	 * 0 no action 
	 * 1 checking version 
	 * 2 downloading
	 */
	private int operState = 0;
	
	private String softSid="";
	private String softVersion="";
	private String coSid="";

	public MainFrame(String coSid, String softSid, String softVersion) {
		super("安之信软件通用升级器1.0 Beta版");
		this.coSid = coSid;
		this.softSid=softSid;
		this.softVersion = softVersion;
		initComponents();
	}

	private void initComponents() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.btnDown = new JButton("升级");
		this.btnDown.addActionListener(new DownLoadActionListener(this));
		this.btnExit = new JButton("退出");
		this.btnDownloadDetail = new JButton("显示 / 隐藏详情");
		this.btnDownloadDetail.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(logScrollPane.isVisible()){
					logScrollPane.setVisible(false);
					setSize(500, 185);
				}else{
					logScrollPane.setVisible(true);
					setSize(500, 280);
				}
			}
		});
		this.btnExit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (operState == 0) {
					System.exit(0);
				} else if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(MainFrame.this, "退出将中断当前操作:"
						+ (operState == 1 ? "检测新版本" : "下载") + "，确实要退出吗？", "确认", JOptionPane.YES_NO_OPTION)) {
					System.exit(0);
				}

			}
		});

		this.lblStatus = new JLabel("点升级按钮开始升级");
		this.txtLog.setEditable(false);
		logScrollPane = new JScrollPane(txtLog);
		this.logScrollPane.setVisible(false);
		this.logScrollPane.setBounds(15, 149, 460, 85);
		progressBarAll = new JProgressBar(0, 155);
		progressBarAll.setValue(0);
		progressBarAll.setStringPainted(true);
		progressBarCurr = new JProgressBar(0, 100);
		progressBarCurr.setValue(0);
		progressBarCurr.setStringPainted(true);
		showPanel();
	}

	private void showPanel() {
		String cols = "15px, 320, 5px, p, 15px, p, 15px";
		String rows = "15px, p, 10px, p, 10px, p, 10px, p, 10px";

		FormLayout layout = new FormLayout(cols, rows);
		PanelBuilder builder = new PanelBuilder(layout);

		CellConstraints cc = new CellConstraints();
		builder.add(this.lblStatus, cc.xy(2, 2));
		builder.add(this.btnDown, cc.xy(4, 2));
		builder.add(this.btnExit, cc.xy(6, 2));
		builder.add(this.progressBarCurr, cc.xyw(2, 4, 5));
		builder.add(this.progressBarAll, cc.xyw(2, 6, 5));
		builder.add(this.btnDownloadDetail, cc.xyw(4, 8,3));
		this.add(this.logScrollPane);
		this.add(builder.getPanel());
		pack();
//		this.setSize(500, 160);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		setVisible(true);
	}

	public static void main(String[] args) {
		if(args==null||args.length<3){
			JOptionPane.showMessageDialog(null, "升级程序不允许独立运行，请从相关程序中启动升级程序！");
			return;
		}
		new MainFrame(args[0],args[1],args[2]);
	}

	public JButton getBtnDown() {
		return btnDown;
	}

	public int getOperState() {
		return operState;
	}

	public void setOperState(int operState) {
		this.operState = operState;
	}

	public JProgressBar getProgressBarAll() {
		return progressBarAll;
	}

	public JProgressBar getProgressBarCurr() {
		return progressBarCurr;
	}

	public String getSoftSid() {
		return softSid;
	}

	public String getSoftVersion() {
		return softVersion;
	}

	public JLabel getLblStatus() {
		return lblStatus;
	}

	public JTextArea getTxtLog() {
		return txtLog;
	}

}